package com.openbravo.pos.panels;

import com.openbravo.basic.BasicException;
import com.openbravo.data.gui.ComboBoxValModel;
import com.openbravo.data.loader.IKeyed;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.data.user.EditorRecord;
import com.openbravo.editor.JEditorCurrency;
import com.openbravo.editor.JEditorKeys;
import com.openbravo.pos.forms.AppLocal;
import com.openbravo.pos.forms.AppView;

import java.awt.BorderLayout;
import java.awt.Component;
import java.util.Date;
import java.util.UUID;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.LayoutStyle;
import javax.swing.LayoutStyle.ComponentPlacement;

public class PaymentsEditor extends JPanel
  implements EditorRecord
{
  private ComboBoxValModel m_ReasonModel;
  private String m_sId;
  private String m_sPaymentId;
  private Date datenew;
  private AppView m_App;
  private JLabel jLabel3;
  private JLabel jLabel5;
  private JPanel jPanel2;
  private JPanel jPanel3;
  private JEditorCurrency jTotal;
  private JEditorKeys m_jKeys;
  private JComboBox m_jreason;

  public PaymentsEditor(AppView paramAppView, DirtyManager paramDirtyManager)
  {
    this.m_App = paramAppView;
    initComponents();
    this.m_ReasonModel = new ComboBoxValModel();
    this.m_ReasonModel.add(new PaymentReasonPositive("cashin", AppLocal.getIntString("transpayment.cashin")));
    this.m_ReasonModel.add(new PaymentReasonNegative("cashout", AppLocal.getIntString("transpayment.cashout")));
    this.m_jreason.setModel(this.m_ReasonModel);
    this.jTotal.addEditorKeys(this.m_jKeys);
    this.m_jreason.addActionListener(paramDirtyManager);
    this.jTotal.addPropertyChangeListener("Text", paramDirtyManager);
    writeValueEOF();
  }

  public void writeValueEOF()
  {
    this.m_sId = null;
    this.m_sPaymentId = null;
    this.datenew = null;
    setReasonTotal(null, null);
    this.m_jreason.setEnabled(false);
    this.jTotal.setEnabled(false);
  }

  public void writeValueInsert()
  {
    this.m_sId = null;
    this.m_sPaymentId = null;
    this.datenew = null;
    setReasonTotal("cashin", null);
    this.m_jreason.setEnabled(true);
    this.jTotal.setEnabled(true);
    this.jTotal.activate();
  }

  public void writeValueDelete(Object paramObject)
  {
    Object[] arrayOfObject = (Object[])(Object[])paramObject;
    this.m_sId = ((String)arrayOfObject[0]);
    this.datenew = ((Date)arrayOfObject[2]);
    this.m_sPaymentId = ((String)arrayOfObject[3]);
    setReasonTotal(arrayOfObject[4], arrayOfObject[5]);
    this.m_jreason.setEnabled(false);
    this.jTotal.setEnabled(false);
  }

  public void writeValueEdit(Object paramObject)
  {
    Object[] arrayOfObject = (Object[])(Object[])paramObject;
    this.m_sId = ((String)arrayOfObject[0]);
    this.datenew = ((Date)arrayOfObject[2]);
    this.m_sPaymentId = ((String)arrayOfObject[3]);
    setReasonTotal(arrayOfObject[4], arrayOfObject[5]);
    this.m_jreason.setEnabled(false);
    this.jTotal.setEnabled(false);
    this.jTotal.activate();
  }

  public Object createValue()
    throws BasicException
  {
    Object[] arrayOfObject = new Object[6];
    arrayOfObject[0] = (this.m_sId == null ? UUID.randomUUID().toString() : this.m_sId);
    arrayOfObject[1] = this.m_App.getActiveCashIndex();
    arrayOfObject[2] = (this.datenew == null ? new Date() : this.datenew);
    arrayOfObject[3] = (this.m_sPaymentId == null ? UUID.randomUUID().toString() : this.m_sPaymentId);
    arrayOfObject[4] = this.m_ReasonModel.getSelectedKey();
    PaymentReason localPaymentReason = (PaymentReason)this.m_ReasonModel.getSelectedItem();
    Double localDouble = this.jTotal.getDoubleValue();
    arrayOfObject[5] = (localPaymentReason == null ? localDouble : localPaymentReason.addSignum(localDouble));
    return arrayOfObject;
  }

  public Component getComponent()
  {
    return this;
  }

  public void refresh()
  {
  }

  private void setReasonTotal(Object paramObject1, Object paramObject2)
  {
    this.m_ReasonModel.setSelectedKey(paramObject1);
    PaymentReason localPaymentReason = (PaymentReason)this.m_ReasonModel.getSelectedItem();
    if (localPaymentReason == null)
      this.jTotal.setDoubleValue((Double)paramObject2);
    else
      this.jTotal.setDoubleValue(localPaymentReason.positivize((Double)paramObject2));
  }

  private void initComponents()
  {
    this.jPanel3 = new JPanel();
    this.jLabel5 = new JLabel();
    this.m_jreason = new JComboBox();
    this.jLabel3 = new JLabel();
    this.jTotal = new JEditorCurrency();
    this.jPanel2 = new JPanel();
    this.m_jKeys = new JEditorKeys();
    setLayout(new BorderLayout());
    this.jLabel5.setText(AppLocal.getIntString("label.paymentreason"));
    this.m_jreason.setFocusable(false);
    this.jLabel3.setText(AppLocal.getIntString("label.paymenttotal"));
    GroupLayout localGroupLayout = new GroupLayout(this.jPanel3);
    this.jPanel3.setLayout(localGroupLayout);
    localGroupLayout.setHorizontalGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(localGroupLayout.createSequentialGroup().addContainerGap().addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(this.jLabel5, -2, 150, -2).addComponent(this.jLabel3, -2, 150, -2)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(this.m_jreason, -2, 200, -2).addComponent(this.jTotal, -2, 234, -2)).addContainerGap(63, 32767)));
    localGroupLayout.setVerticalGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(localGroupLayout.createSequentialGroup().addContainerGap().addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.jLabel5).addComponent(this.m_jreason, -2, -1, -2)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.TRAILING).addComponent(this.jTotal, -2, -1, -2).addComponent(this.jLabel3)).addContainerGap(320, 32767)));
    add(this.jPanel3, "Center");
    this.jPanel2.setLayout(new BorderLayout());
    this.jPanel2.add(this.m_jKeys, "North");
    add(this.jPanel2, "After");
  }

  private static class PaymentReasonNegative extends PaymentsEditor.PaymentReason
  {
    public PaymentReasonNegative(String paramString1, String paramString2)
    {
      super(paramString1,paramString2);//ati add the aram1  ,ori only para2
    }

    public Double positivize(Double paramDouble)
    {
      return paramDouble == null ? null : new Double(-paramDouble.doubleValue());
    }

    public Double addSignum(Double paramDouble)
    {
      if (paramDouble == null)
        return null;
      if (paramDouble.doubleValue() > 0.0D)
        return new Double(-paramDouble.doubleValue());
      return paramDouble;
    }
  }

  private static class PaymentReasonPositive extends PaymentsEditor.PaymentReason
  {
    public PaymentReasonPositive(String paramString1, String paramString2)
    {
    	//ati add the param1 param
      super(paramString1,paramString2);
    }

    public Double positivize(Double paramDouble)
    {
      return paramDouble;
    }

    public Double addSignum(Double paramDouble)
    {
      if (paramDouble == null)
        return null;
      if (paramDouble.doubleValue() < 0.0D)
        return new Double(-paramDouble.doubleValue());
      return paramDouble;
    }
  }

  private static abstract class PaymentReason
    implements IKeyed
  {
    private String m_sKey;
    private String m_sText;

    public PaymentReason(String paramString1, String paramString2)
    {
      this.m_sKey = paramString1;
      this.m_sText = paramString2;
    }

    public Object getKey()
    {
      return this.m_sKey;
    }

    public abstract Double positivize(Double paramDouble);

    public abstract Double addSignum(Double paramDouble);

    public String toString()
    {
      return this.m_sText;
    }
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.pos.panels.PaymentsEditor
 * JD-Core Version:    0.6.0
 */